'use strict'
import { observable, action } from 'mobx'
// import Cookies from 'js-cookie'
import asyncFetch from '../config/fetch'
import config from '../config/config'
const roomURL = config.resHeader + '/v1/doll/touch/room'
class Room {
  @observable videos = []
  @observable dollImage = ''
  @observable dollMusic = ''
  @observable videosPos = -1
  @observable canvasPos = 0
  @observable gameState = -1 // -3  -2 -1 0 1 2 维护中 别人玩  未开始，点击开始并且确认可以开始 , 链接腾讯SDK ， 真正开始
  @observable userInfo = {}
  @observable dollDetail = {}
  @observable txDetail = {}
  @observable recordList = []
  @observable chatList = []
  @observable inRoomNumber = 0
  @observable coin = 0
  @observable resultState = 0
  @observable playerMsg = null
  @observable noMoneyState = 0
  _loadImage (img) {
    const image = new Image()
    image.src = img
  }
  // 获取房间信息
  @action getRoomMsg = async (anchorID, roomID, cb) => {
    window.Qapp.showLoad('加载中...')
    const res = await asyncFetch(roomURL, [{ anchor_id: anchorID, room_id: roomID }])
    if (res.err === 0) {
      this.videos = [`http://2288.liveplay.myqcloud.com/live/${res.tx_detail.side_url}.m3u8`, `http://2288.liveplay.myqcloud.com/live/${res.tx_detail.pull_url}.m3u8`]
      this.dollImage = res.doll_detail.doll_pic
      this.dollMusic = res.doll_detail.doll_music_url
      this.userInfo = res.userInfo
      this.dollDetail = res.doll_detail
      this.txDetail = {
        sdkappid: res.sdkappid,
        accounttype: res.accounttype,
        identifier: res.identifier,
        usersig: res.usersig,
        front: res.tx_detail.anchor_id,
        side: res.tx_detail.side_anchor_id,
        sig: res.sig,
        ver: res.ver,
        roomId: res.room_id
      }
      this.recordList = res.catch_his
      this.coin = res.coin
      this._loadImage(res.doll_detail.doll_pic_large)
      cb()
    }
    else {
      window.Qapp.hideLoad()
      window.Qapp.showToast(res.err_msg)
    }
  }
  // 切换镜头：根据gameState 去切换 video ／ canvas
  @action changeVideoPos = (videos) => {
    // 旁观视频切换
    if (this.gameState !== 2) {
      this.videosPos = this.videosPos === 0 ? 1 : 0
      if (videos.length === 2) { videos[this.videosPos].play() }
      if (this.videosPos === -1) { this.videosPos = 0 }
    }
    // 游戏视频切换
    else {
      this.canvasPos = this.canvasPos === 0 ? 1 : 0
      if (this.canvasPos === 0) { TXWawaVideo.renderFront() }
      else if (this.canvasPos === 1) { TXWawaVideo.renderSide() }
    }
  }
  // 修改gameState 状态
  @action changeGameState = (state, time) => {
    this.gameState = state
    if (state === 2 || state === -1) {
      this.videosPos = 0
      this.canvasPos = 0
    }
    if (time && window.doll) {
      setTimeout(() => { window.doll.startLoop(time) }, 1000)
    }
  }
  // 清空所有数据
  @action clearVideos = function () {
    this.videos = []
    this.dollImage = ''
    this.dollMusic = ''
    this.videosPos = -1
    this.canvasPos = 0
    this.gameState = -1 // -2 -1 0 1 2 别人玩  未开始，点击开始并且确认可以开始 , 链接腾讯SDK ， 真正开始
    this.userInfo = {}
    this.dollDetail = {}
    this.txDetail = {}
    this.recordList = []
    this.chatList = []
    this.inRoomNumber = 0
    this.coin = 0
    this.resultState = 0
    this.playerMsg = null
    this.noMoneyState = 0
  }
  // 聊天信息增加
  @action addChat = (chat) => {
    let chatList = this.chatList
    chatList = chatList.slice()
    chatList.push(chat)
    this.chatList = chatList
  }
  // 房间在线人数修改
  @action changeInRoomNumber = (number) => { this.inRoomNumber = number }
  // 修改房间结果弹窗状态
  @action changeRoomState = (state) => { this.resultState = state }
  // 玩家信息
  @action changePlayerMsg = (msg) => {
    this.changeGameState(-2)
    this.playerMsg = msg
  }
  // 没有余额弹窗
  @action changenoMoneyState =() => {
    this.noMoneyState = this.noMoneyState === 0 ? 1 : 0
  }
  // 修改余额
  @action changeCoin = (coin) => { this.coin = coin }
}

export default new Room()
